﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.CharmManager.Client.Interface;


namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class frmTodayReservationCustom : FormBase
    {
        /// <summary>
        /// 当前预约的会员
        /// </summary>
        private DataRow customRow = null;
        /// <summary>
        /// 预约记录
        /// </summary>
        private DataRow reservationRow = null;
        /// <summary>
        /// 预约项目关系表
        /// </summary>
        private DataTable dtReservationDetail = null;
        /// <summary>
        /// 预约主记录
        /// </summary>
        private DBEntityReservation reservationInfo = null;

        private ReservationCtrl reservationCtrl = new ReservationCtrl();
        /// <summary>
        /// 员工信息
        /// </summary>
        private DataSet dsEmployee = new DataSet();
        /// <summary>
        /// 员工ID 和显示MAP
        /// </summary>
        private SortedList<int, string> srotEmployeeName = new SortedList<int, string>();

        public frmTodayReservationCustom() : base()
        {
            InitializeComponent();
            cbxState.SelectedItem = cbxItemC0N;
            initData();
            eventSerchCustom(customRow);
            hourTimeSimpleControl.OnTimeChange = OnhourTimeSimpleControlTimeChange;
            //customSearch.onSerchCustom += OnEventSerchCustom;
        }

        public frmTodayReservationCustom(DataRow drCustomInfo, DataRow drReservation):this()
        {
            customRow = drCustomInfo;
            reservationRow = drReservation;
            //预约说明
            txtRMemo.Text = drReservation[DBEntityReservation.FieldName.RMEMO].ToString();
            reservationInfo = new DBEntityReservation(drReservation);
            initFrom(reservationInfo);
            eventSerchCustom(drCustomInfo);
        }

        private void initFrom(DBEntityReservation reservationInfo)
        {
            string error = string.Empty;
            DataSet dsResverationServiceItem = new DataSet();
            if (!BussinessProxyManager.Instance.QueryReservationServiceItem(reservationInfo.RESERVATIONID, out dsResverationServiceItem, out error))
            {
                MsgBox.MsgInfo("初始化预约项目错误：" + error);
                return;
            }
            dsResverationServiceItem.AcceptChanges();
            dtReservationDetail = dsResverationServiceItem.Tables[0].Copy();

        }
        /// <summary>
        /// 初始化数据
        /// </summary>
        private void initData()
        {
            dateInputReservation.Value = DateTime.Now;
            customSearch.onSerchCustom += eventSerchCustom;
            hourTimeSimpleControl.CurrDateTime = DateTime.Now;
            DataSet dsResverationServiceItem = new DataSet();
            string error = string.Empty;
            if (!BussinessProxyManager.Instance.QueryReservationServiceItem(-1, out dsResverationServiceItem, out error))
            {
                MsgBox.MsgInfo("初始化预约项目错误：" + error);
                return;
            }
            if (!BussinessProxyManager.Instance.QueryEmployeeState(string.Empty, out dsEmployee, out error))
            {
                MsgBox.MsgInfo("初始化员工信息错误：" + error);
                return;
            }
            foreach (DataRow drEmployee in dsEmployee.Tables[0].Rows)
            {
                //初始化员工信息
                repositoryItemComboBoxEmployeeName.Items.Add(drEmployee[DBEntityEmployee.FieldName.ENAME].ToString());
                srotEmployeeName.Add(Convert.ToInt32(drEmployee[DBEntityEmployee.FieldName.EMPLOYEEID]), drEmployee[DBEntityEmployee.FieldName.ENAME].ToString());
            }
            dsResverationServiceItem.AcceptChanges();
            dtReservationDetail = dsResverationServiceItem.Tables[0].Copy();
            gdServiceItem.DataSource = dtReservationDetail;
        }
        /// <summary>
        /// 根据用户时间初始化界面
        /// </summary>
        /// <param name="customInfo">用户信息</param>
        /// <param name="reservationTime">预约时间</param>
        private void initFrmData(DBEntityCustomInfo customInfo, DateTime reservationTime)
        {
            string error = string.Empty;
            DataSet dsReservation = new DataSet();
            if (!BussinessProxyManager.Instance.QueryReservationCustom(reservationTime.Year, reservationTime.Month, reservationTime.Day, customInfo.CUSTOMERID, out dsReservation, out error))
            {
                MsgBox.MsgInfo("获取预约信息错误：" + error);
                return;
            }
            if (dsReservation.Tables[0].Rows.Count == 0)
            {
                //没有记录跳出
                return;
            }
            reservationInfo = new DBEntityReservation(dsReservation.Tables[0].Rows[0]);
            reservationRow = dsReservation.Tables[0].Rows[0];
            initFrom(reservationInfo);

            DataSet dsResverationServiceItem = new DataSet();
            if (!BussinessProxyManager.Instance.QueryReservationServiceItem(reservationInfo.RESERVATIONID, out dsResverationServiceItem, out error))
            {
                MsgBox.MsgInfo("初始化预约项目错误：" + error);
                return;
            }
            dsResverationServiceItem.AcceptChanges();
            dtReservationDetail = dsResverationServiceItem.Tables[0].Copy();
            gdServiceItem.DataSource = dtReservationDetail;
        }
        /// <summary>
        /// 查询后
        /// </summary>
        /// <param name="drCustomInfo"></param>
        private void eventSerchCustom(DataRow drCustomInfo){
            if (drCustomInfo == null)
                return;

            customRow = drCustomInfo;
            labCustomName.Text = "会员：" + customRow[DBEntityCustomInfo.FieldName.CNAME].ToString();
            labNickName.Text = "昵称：" + customRow[DBEntityCustomInfo.FieldName.CNICKNAME].ToString();
            labTelPhone.Text = "电话：" + customRow[DBEntityCustomInfo.FieldName.CMOBILEPHONE].ToString();
            labLastRegister.Text = "上次到店：" + Convert.ToDateTime(customRow[DBEntityCustomInfo.FieldName.CLASTREGISTERDATE]).ToShortDateString()
                + " " + Convert.ToDateTime(customRow[DBEntityCustomInfo.FieldName.CLASTREGISTERDATE]).ToShortTimeString();
            if (customRow != null)
            {
                DBEntityCustomInfo customInfo = new DBEntityCustomInfo(customRow);
                //根据用户查询当前预约
                initFrmData(customInfo, getReservationDateTime());
            }
        }

        private void textBoxItem1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.CompareTo('\r') == 0)
            {
                //MsgBox.MsgInfo("调用服务项目进行快速添加！！");
                string sKeyword = textBoxItem1.TextBox.Text.Trim(); 
                if (customRow == null)
                {
                    MsgBox.MsgInfo("请选择预约的会员。");
                    SetErrorControl(labCustomName, "请选择预约的会员");
                    return;
                }
                int iCustomerID = Convert.ToInt32(customRow[DBEntityCustomInfo.FieldName.CUSTOMERID]);
                ServiceItemCCtrl cc = new ServiceItemCCtrl();
                //DBEntityCustomerServiceItem item = cc.GetCustomerServiceItem(iCustomerID, sKeyword);
                DBEntityCustomerServiceItem item = ServiceItemCCtrl.GetCustomerServiceItemByIndexCode(iCustomerID, sKeyword);
                if (item == null)
                {
                    return;
                }
                string error = string.Empty;
                if (!checkReservationCustomServiceItem(item.CUSTOMERSERVICEITEMID, iCustomerID, out error))
                {
                    MsgBox.MsgInfo(error);
                    return;
                }
                if (!checkReservationCustomServiceItem(item.CUSTOMERSERVICEITEMID, dtReservationDetail))
                {
                    MsgBox.MsgInfo("该项目已经预约，请选择其它项目进行预约。");
                    return;
                }
                
                if (!checkCurrEmployeeReservation(dtReservationDetail, out error))
                {
                    MsgBox.MsgInfo(error);
                    return;
                }
                addReservationCustomServiceItem(item);
            }
        }

        private bool checkInput(out string error)
        {
            error = string.Empty;
            if (customRow == null)
            {
                error = "请选择预约的会员。";
                SetErrorControl(labCustomName, "请选择预约的会员");
                return false;
            }
            gvServiceItem.UpdateCurrentRow();
            if (gvServiceItem.RowCount == 0)
            {
                error = "请选择预约项目。";
                SetErrorControl(gdServiceItem, "请选择预约项目");
                return false;
            }

            return checkCurrEmployeeReservation(dtReservationDetail, out error);
            
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string error = string.Empty;
            if (!checkInput(out error))
            {
                MsgBox.MsgInfo(error);
                return;
            }

            DBEntityReservation reservationInfo = new DBEntityReservation();

            reservationInfo.CUSTOMERID = Convert.ToInt32(customRow[DBEntityCustomInfo.FieldName.CUSTOMERID]);
            reservationInfo.RESERVATIONTIME = new DateTime(getReservationDateTime().Ticks);
            //预约到店时间就是操作时间
            reservationInfo.RWORKSTARTTIME = reservationInfo.RESERVATIONTIME;

            //操作完成时间
            reservationInfo.RWORKENDTIME = new DateTime(reservationInfo.RESERVATIONTIME.Ticks);
            reservationInfo.RWORKENDTIME = reservationInfo.RWORKENDTIME.AddMinutes(reservationCtrl.getResverationDetailTimeSpan(dtReservationDetail));

            reservationInfo.RSTATE = DBEntityReservation.ENUM_RSTATE.ChineseStrToEnumStr(cbxState.SelectedItem.ToString());

            reservationInfo.RMEMO = txtRMemo.Text;

            bool bresult = false;
            if (dtReservationDetail.GetChanges() != null)
            {
                DataSet dsReservationDetail = new DataSet();
                dsReservationDetail.Tables.Add(dtReservationDetail);


                if (reservationRow == null)
                {
                    //插入
                    bresult = BussinessProxyManager.Instance.AddResveration(reservationInfo, dsReservationDetail, out error);
                }
                else
                {
                    //更新
                    reservationInfo.RESERVATIONID = Convert.ToInt32(reservationRow[DBEntityReservation.FieldName.RESERVATIONID]);
                    DataRow[] drReservationDetail = dsReservationDetail.Tables[0].Select("", "", DataViewRowState.CurrentRows);
                    foreach (DataRow dr in drReservationDetail)
                    {
                        dr[DBEntityReservation.FieldName.RESERVATIONID] = reservationInfo.RESERVATIONID;
                    }
                    bresult = BussinessProxyManager.Instance.UpdateReservation(reservationInfo, dsReservationDetail, out error);
                }
                dsReservationDetail.AcceptChanges();

                if (!bresult)
                {
                    MsgBox.MsgInfo("保存失败错误信息为：" + error);
                    return;
                }

                MsgBox.MsgInfo("保存预约记录成功。");
                DateTime callTime = new DateTime(reservationInfo.RESERVATIONTIME.AddDays(-1).Ticks);
                //如果当前天不是预约的前一天那么插入一条待办事项。
                if (callTime.ToShortDateString() != DateTime.Now.ToShortDateString())
                {
                    if (!BussinessProxyManager.Instance.QueryResverationID(reservationInfo, out error))
                    {
                        MsgBox.MsgInfo("获取最新预约ID错误：" + error);
                        return;
                    }
                    DBEntityCustomInfo customInfo = new DBEntityCustomInfo(this.customRow);
                    string serviceName = "";
                    if (checkBoxEmployee.Checked)
                    {
                        int employeeID = -1;
                        foreach (DataRow dr in dsReservationDetail.Tables[0].Rows)
                        {
                            serviceName += " [" + dr[DBEntityServiceItem.FieldName.SERVICEITEMCODE].ToString() + " - " +
                            dr[DBEntityServiceItem.FieldName.SNAME].ToString() + "] ";
                            employeeID = Convert.ToInt32(dr[DBEntityEmployee.FieldName.EMPLOYEEID]);
                        }
                        //一个美容师
                        bresult = reservationCtrl.InsertToDoList(employeeID, customInfo, reservationInfo, serviceName, out error);
                    }
                    else
                    {
                        //用来存储多个美容师预约信息
                        SortedList<int, string> serviceItemInfo = new SortedList<int, string>();
                        foreach (DataRow dr in dsReservationDetail.Tables[0].Rows)
                        {
                            int idx = serviceItemInfo.IndexOfKey(Convert.ToInt32(dr[DBEntityEmployee.FieldName.EMPLOYEEID]));
                            if (idx < 0)
                            {
                                serviceItemInfo.Add(Convert.ToInt32(dr[DBEntityEmployee.FieldName.EMPLOYEEID]), " [" + dr[DBEntityServiceItem.FieldName.SERVICEITEMCODE].ToString() + " - " +
                                dr[DBEntityServiceItem.FieldName.SNAME].ToString() + "] ");
                            }
                            else
                            {
                                serviceName = serviceItemInfo[Convert.ToInt32(dr[DBEntityEmployee.FieldName.EMPLOYEEID])].ToString() + " [" + dr[DBEntityServiceItem.FieldName.SERVICEITEMCODE].ToString() + " - " +
                                dr[DBEntityServiceItem.FieldName.SNAME].ToString() + "] ";
                                serviceItemInfo.RemoveAt(idx);
                                serviceItemInfo.Add(Convert.ToInt32(dr[DBEntityEmployee.FieldName.EMPLOYEEID]), serviceName);
                            }
                        }
                        for (int i = 0; i < serviceItemInfo.Count; i++)
                        {
                            //一个美容师
                            bresult = reservationCtrl.InsertToDoList(serviceItemInfo.Keys[i], customInfo, reservationInfo, serviceItemInfo.Values[i], out error);
                            if (!bresult)
                                break;
                        }
                    }
                    if (!bresult)
                    {
                        MsgBox.MsgInfo("生成代办事项错误：" + error);
                        return;
                    }
                }

            }
            this.DialogResult = DialogResult.OK;
        }

        private void gvServiceItem_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            if (e.RowHandle < 0)
                return;

            gvServiceItem.UpdateCurrentRow();
            DataRow drDetail = gvServiceItem.GetDataRow(e.RowHandle);
            if (drDetail[DBEntityReservationServiceItem.FieldName.EMPLOYEEID] == null )
            {
                if (checkBoxEmployee.Checked)
                {//同一个美容师
                    DataRow[] drEmployee = dtReservationDetail.Select(DBEntityReservationServiceItem.FieldName.EMPLOYEEID + " IS NOT NULL");
                    if (drEmployee.Length != 0)
                    {
                        drDetail[DBEntityReservationServiceItem.FieldName.EMPLOYEEID] = drEmployee[0][DBEntityReservationServiceItem.FieldName.EMPLOYEEID];
                        return;
                    }
                }
                e.ErrorText = "请选择预约的美容师";
                e.Valid = false;
            }
        }
        /// <summary>
        /// 获取界面预约开始时间
        /// </summary>
        /// <returns></returns>
        private DateTime getReservationDateTime()
        {
            DateTime dt = new DateTime(dateInputReservation.Value.Year, dateInputReservation.Value.Month, dateInputReservation.Value.Day,
                hourTimeSimpleControl.CurrDateTime.Hour, hourTimeSimpleControl.CurrDateTime.Minute, hourTimeSimpleControl.CurrDateTime.Second);
            return dt;
        }
        private void addServiceItem_Click(object sender, EventArgs e)
        {
            if (customRow == null)
            {
                MsgBox.MsgInfo("请选择预约的会员。");
                SetErrorControl(labCustomName, "请选择预约的会员");
                return;
            }
            string error = string.Empty;
            int iCustomerID = Convert.ToInt32(customRow[DBEntityCustomInfo.FieldName.CUSTOMERID]);
            ServiceItemCCtrl cc = new ServiceItemCCtrl();
            DBEntityCustomerServiceItem item = ServiceItemCCtrl.GetCustomerServiceItemByIndexCode(iCustomerID, string.Empty);
            if (item == null)
            {
                return;
            }

            if (!checkReservationCustomServiceItem(item.CUSTOMERSERVICEITEMID, dtReservationDetail))
            {
                MsgBox.MsgInfo("该项目已经预约，请选择其它项目进行预约。");
                return;
            }

            if (!checkReservationCustomServiceItem(item.CUSTOMERSERVICEITEMID, iCustomerID, out error))
            {
                MsgBox.MsgInfo(error);
                return;
            }

            if (!checkCurrEmployeeReservation(dtReservationDetail, out error))
            {
                MsgBox.MsgInfo(error);
                return;
            }

            addReservationCustomServiceItem(item);
        }

        private void addReservationCustomServiceItem(DBEntityCustomerServiceItem item)
        {
            string error = string.Empty;
            DataSet dsServiceItem = new DataSet();
            if (!BussinessProxyManager.Instance.QueryServiceItem(DBEntityServiceItem.FieldName.SERVICEITEMID + " = "
                + item.SERVICEITEMID.ToString(), out dsServiceItem, out error))
            {
                MsgBox.MsgInfo("获取服项目错误：" + error);
                return;
            }

            DBEntityServiceItem serviceItem = new DBEntityServiceItem(dsServiceItem.Tables[0].Rows[0]);

            DataRow drNull = null;
            reservationCtrl.addReservationDetail(dtReservationDetail, serviceItem, item, ref drNull);

            DateTime dt = getReservationDateTime();

            refershOverTimeText(dt, dtReservationDetail);
            gdServiceItem.DataSource = dtReservationDetail;
        }

        private void refershOverTimeText(DateTime dt, DataTable dtDetail)
        {
            labOverTime.Text = "完成时间：" + dt.AddMinutes(reservationCtrl.getResverationDetailTimeSpan(dtDetail)).ToLongTimeString();
        }
       
        /// <summary>
        /// 时间改变完成时间进行刷新
        /// </summary>
        /// <param name="dt"></param>
        private void OnhourTimeSimpleControlTimeChange(DateTime dt)
        {
            DateTime dtTime = getReservationDateTime();
            if (customRow != null)
            {
                DBEntityCustomInfo customInfo = new DBEntityCustomInfo(customRow);
                //根据用户查询当前预约
                initFrmData(customInfo, getReservationDateTime());
            }
            refershOverTimeText(dtTime, dtReservationDetail);
        }
        /// <summary>
        /// 检测美容师是否休假
        /// </summary>
        /// <param name="drReservationDetail"></param>
        /// <returns></returns>
        private bool checkEmployeeVacation(DataTable drReservationDetail)
        {
            StringBuilder superStr = new StringBuilder();
            DataRow[] drDetails = dtReservationDetail.Select("", "", DataViewRowState.CurrentRows);
            return true;
        }
        /// <summary>
        /// 对当前的预约记录进行判断是否有预约的美容师
        /// </summary>
        /// <param name="dtReservationDetail"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        private bool checkCurrEmployeeReservation(DataTable dtReservationDetail, out string error){
            error = string.Empty;
            if (dtReservationDetail.GetChanges() == null)
            {
                //没有修改记录不用处理
                return true;
            }
            DataRow[] drDetails = dtReservationDetail.Select("", DBEntityEmployee.FieldName.EMPLOYEEID + " DESC", DataViewRowState.CurrentRows);
            int employeeID = -1;
            foreach (DataRow dr in drDetails)
            {
                if (checkBoxEmployee.Checked && employeeID == -1)
                {
                    if (dr[DBEntityEmployee.FieldName.EMPLOYEEID] != System.DBNull.Value)
                    {
                        employeeID = Convert.ToInt32(dr[DBEntityEmployee.FieldName.EMPLOYEEID]);
                    }
                    else
                    {
                        error = "请选择预约的美容师。";
                        return false;
                    }
                }
                if (checkBoxEmployee.Checked)
                {
                    dr[DBEntityEmployee.FieldName.EMPLOYEEID] = employeeID;
                }
                else if (dr[DBEntityEmployee.FieldName.EMPLOYEEID] == System.DBNull.Value)
                {
                    error = "请选择预约的美容师。";
                    return false;
                }
            }
            return true;
        }
        private void gvServiceItem_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.RowHandle < 0)
            {
                return;
            }
            if (e.Column.FieldName.Equals(DBEntityEmployee.FieldName.EMPLOYEEID))
            {
                if (e.CellValue == null || e.CellValue.ToString() == string.Empty)
                    return;

                string displayText = srotEmployeeName[Convert.ToInt32(e.CellValue)].ToString();
                if (displayText.Equals(string.Empty))
                    return;
                e.DisplayText = displayText;
            }
        }

        private void repositoryItemComboBoxEmployeeName_SelectedValueChanged(object sender, EventArgs e)
        {
            DataRow drRow = gvServiceItem.GetDataRow(gvServiceItem.FocusedRowHandle);
            string strValue = ((DevExpress.XtraEditors.ComboBoxEdit)sender).SelectedItem.ToString();
            DataRow[] drEmployees = dsEmployee.Tables[0].Select(DBEntityEmployee.FieldName.ENAME + " = '" + strValue + "'");
            if (drEmployees.Length != 0)
            {
                drRow[DBEntityEmployee.FieldName.EMPLOYEEID] = drEmployees[0][DBEntityEmployee.FieldName.EMPLOYEEID];
            }
        }

        private void delServiceItem_Click(object sender, EventArgs e)
        {
            if (gvServiceItem.FocusedRowHandle < 0)
            {
                return;
            }
            string error = string.Empty;
            DBEntityCustomInfo customInfo = new DBEntityCustomInfo(customRow);
            DataRow drCustomServiceItem = gvServiceItem.GetDataRow(gvServiceItem.FocusedRowHandle);
            DataSet dsCustomServiceItem = new DataSet();
            if(!BussinessProxyManager.Instance.QueryReservationCustomServiceItemFilter(DBEntityReservationServiceItem.FieldName.CUSTOMERSERVICEITEMID + " = " 
                +  drCustomServiceItem[DBEntityReservationServiceItem.FieldName.CUSTOMERSERVICEITEMID].ToString(), out dsCustomServiceItem, out error))
            {
                MsgBox.MsgInfo("获取预约信息错误：" + error);
                return;
            }

            if (dsCustomServiceItem.Tables[0].Rows.Count > 0)
            {
                MsgBox.MsgInfo("此服务项目已经进入预约状态，项目不能够删除只能进行调整。");
                return;
            }
            
            //当前预约项目
            gvServiceItem.DeleteRow(gvServiceItem.FocusedRowHandle);
            gvServiceItem.UpdateCurrentRow();

            refershOverTimeText(getReservationDateTime(), dtReservationDetail);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonItem1_Click(object sender, EventArgs e)
        {
            string error = string.Empty;
            if (gvServiceItem.FocusedRowHandle < 0)
            {
                MsgBox.MsgInfo("请选择要调整的服务项目。");
                return;
            }
            if(customRow == null){
                MsgBox.MsgInfo("请输入正确的会员。");
                return;
            }
            if (!checkCurrEmployeeReservation(dtReservationDetail, out error))
            {
                MsgBox.MsgInfo(error);
                return;
            }
            if (!MsgBox.MsgQue("进行调整前必须保存数据，是否要保存数据？"))
            {
                return;
            }
            //保存数据
            btnSave_Click(sender, e);
            DBEntityCustomInfo customInfo = new DBEntityCustomInfo(customRow);
            DataRow drServiceItemInfo = gvServiceItem.GetDataRow(gvServiceItem.FocusedRowHandle);
            frmAdjustReservationServiceItem frmServiceItem = new frmAdjustReservationServiceItem(drServiceItemInfo, reservationInfo, customInfo);
            if (frmServiceItem.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            
            if (!BussinessProxyManager.Instance.AdjustReservationCustomServiceItem(reservationInfo.RESERVATIONID, frmServiceItem.AdjustDateTime,
                Convert.ToInt16(drServiceItemInfo[DBEntityCustomerServiceItem.FieldName.CUSTOMERSERVICEITEMID]), customInfo.CUSTOMERID, out error))
            {
                MsgBox.MsgInfo("调整项目失败：" + error);
                return;
            }
            initFrmData(customInfo, getReservationDateTime());

        }

        private void frmTodayReservationCustom_Shown(object sender, EventArgs e)
        {
            ButtonItemIconHelper.InitRibbonBar(this.ribbonBar1);
        }

        /// <summary>
        /// 检测当前预约是否重复
        /// </summary>
        /// <param name="customServiceItemID"></param>
        /// <returns></returns>
        private bool checkReservationCustomServiceItem(int customServiceItemID, DataTable dtCustomServiceItem)
        {
            DataRow[] drRow = dtCustomServiceItem.Select(DBEntityReservationServiceItem.FieldName.CUSTOMERSERVICEITEMID + " = " + customServiceItemID.ToString(), "", DataViewRowState.CurrentRows);
            if (drRow.Length == 0)
            {
                //当前没有预约
                return true;
            }
            else
            {
                //有预约
                return false;
            }
        }

        private bool checkReservationCustomServiceItem(int customServiceItemID, int iCustomerID, out string error)
        {
            error = string.Empty;
            string sfilter = string.Format("RSTATE IN ('{0}', '{1}') AND CUSTOMERID = {2} AND CUSTOMERSERVICEITEMID = {3}",  DBEntityReservation.ENUM_RSTATE.C0N,
                DBEntityReservation.ENUM_RSTATE.C0D, iCustomerID, customServiceItemID);
            DataSet dsReservationServiceItem = new DataSet();
            if (!BussinessProxyManager.Instance.QueryReservationCustomServiceItemFilter(sfilter, out dsReservationServiceItem, out error))
            {
                return false;
            }
            if (dsReservationServiceItem.Tables[0].Rows.Count > 0)
            {
                error = "该项目已经在：" + Convert.ToDateTime(dsReservationServiceItem.Tables[0].Rows[0][DBEntityReservation.FieldName.RESERVATIONTIME]).ToShortDateString() + " 日进行预约，请调整预约即可。";
                return false;
            }
            return true;
        }
    }
}
